// 将对象{'a_b_cd_e':1}的键的下划线转化为小驼峰{'aBCdE': 1}
const test = { a_b_cd_e: 1 };

function toCamelCase(obj) {
  const newObj = {};
  for (let key in obj) {
    const newKey = key.replace(/_([a-z])/g, function (match, p1) {
      return p1.toUpperCase();
    });
    newObj[newKey] = obj[key];
  }
  return newObj;
}

function translate(obj) {
  const newObj = {};
  Object.keys(obj).forEach((key) => {
    let isUnderline = false;
    let i = 0;
    let newKey = "";
    while (i < key.length) {
      if (key[i] === "_") {
        isUnderline = true;
        i++;
      } else if (isUnderline) {
        newKey += key[i].toUpperCase();
        i++;
        isUnderline = false;
      } else {
        newKey += key[i];
        i++;
      }
    }
    newObj[newKey] = obj[key];
  });
  return newObj;
}
console.log(translate(test));

function flattenArrayWithDepth(arr, depth) {
  return depth > 0
    ? arr.reduce(
      (acc, val) =>
        acc.concat(
          Array.isArray(val) ? flattenArrayWithDepth(val, depth - 1) : val
        ),
      []
    )
    : arr;
}

/**
 * 构建树形结构
 * @param {Array} arr - 输入的数组
 * @returns {Array} - 构建完成的树形结构数组
 */
function buildTree(arr) {
  const parentMap = new Map();
  const result = [];
  arr.forEach((cur) => {
    if (cur.parentId !== 0) {
      if (parentMap.has(cur.parentId)) {
        const index = parentMap.get(cur.parentId);
        if (!result[index].children) {
          result[index].children = [];
        }
        result[index].children.push(cur);
      } else {
        const parentIndex = result.push({ children: [cur] }) - 1;
        parentMap.set(cur.parentId, parentIndex);
      }
    } else {
      result.push({ ...cur, children: [] });
      parentMap.set(cur.id, result.length - 1);
    }
  });
  return result;
}